

function getCsvFile(){
	addPathSelect("#csvFileChoose","csvpathSelect");
	addPathSelect("#SavePathChoose","pathSelect");
	$("#getCsvFiles").click(function(){
		$.ajax({
			url:"../rest/relation/getFilesFromFolder",
			data :{"path":$("#csvpathSelect").val()},
			method:"POST",
			dataType:"json",
			success:function(data){
				$("#viewBody").html("");
				$.each(data, function(k, v) {
					
				    $("#viewBody").append("<tr><td>"+v+"</td><td><input type='checkbox' class='csvTemplate' value="+k+" /></td></tr>");

				});

				bindCsvTable();

			}
		})
		
	})
}

function createRelation() {
    $("#createRelationFile").click(function() {
	var filePath = $(".csvTemplate:checked").val();
	var operationName = $(".operationName:checked").val();
	var realtionName = $("#relationFileName").val();
	var savePath=$("#pathSelect").val();
	if (filePath != undefined && operationName != undefined && realtionName.length > 0) {
	    $.ajax({
		url:"../rest/relation/createRelation",
		data :{"filePath":filePath,"operationName":operationName,"relationName":realtionName,"savePath":savePath},
		dataType : "text",
		method:"POST",
		success:function(data){
		    $("#messageArea").html("add relation success......");
		},
		error:function(data){
		    $("#messageArea").html("connect error, status:"+data.status+" ,message:"+data.statusText);
		}
	    });
	} else {
	    $("#messageArea").html("please select csv template file and soap method,and add file name");
	}

    });
}

function bindCheckBox(group) {
    group.click(function() {
    	debugger
	var $box = $(this);
	if ($box.is(":checked")) {
	    group.prop("checked", false);
	    $box.prop("checked", true);
	} else {
	    $box.prop("checked", false);
	}
    })
}

function bindCsvTable() {
    bindCheckBox($(".csvTemplate"));
}

function bindSoapTable() {
    bindCheckBox($(".operationName"));
}

function saveWsdl() {
    $("#endpoint-btn").click(function() {
    	// set the message
		$("#messageArea").html("server is running, please wait......")
	
		var sendData = {
		    "endpoint" : $("#endpoint").val()
		};
		$.ajax({
		    url : "../rest/config/endpoint",
		    type : "post",
		    data : sendData,
		    dataType : "text",
		    success : function(data) {
			$("#messageArea").html(data);
			location.reload();
		    },
		    error : function(data) {
			$("#messageArea").html("connect error, status:" + data.status + " ,message:" + data.statusText);
		    }
		});
		
    });
    
}

$(function() {
    saveWsdl();
    getCsvFile();    
    bindSoapTable();
    createRelation();

});
